
import time
from scripts.utils import input_reading, constants 
from dexen.libs import dexen_api, constants as dc

""" GLOBALS """
script_api = dexen_api.getScriptApi()
boxes = input_reading.read_input()
n_boxes = len(boxes)

def is_left(item1, item2):
    if item1['x'] < item2['x'] and item1['y'] < item2['y']:
        return True;
    else:
        return False; 

def is_below(item1, item2):
    if item1['x'] < item2['x'] and item1['y'] > item2['y']:
        return True;
    else:
        return False; 

def calculate(individual):
    """ The indices are not referring to the indices of boxes """
    gamma_x = individual['gamma_x']
    gamma_y = individual['gamma_y']

    """ The indices refers to the actual indices in boxes """
    #phenotype = individual['phenotype']
    encoding = individual['encoding']
    orient = individual['orient']
    phenotype = range(n_boxes)

    """
    Calculates the x coordinates of boxes  
    """
    phenotype[gamma_x[0]] = dict(x=0)
    for i in range(1, n_boxes):
        box1_no = gamma_x[i]
        max_found = 0
        for j in range(0, i):
            box2_no = gamma_x[j]
            side = 0
            if is_left(encoding[box2_no], encoding[box1_no]):
                if orient[box2_no] == 0:
                    side = boxes[box2_no]['w']
                else:
                    side = boxes[box2_no]['h']
                if phenotype[box2_no]['x'] + side > max_found:
                    max_found = phenotype[box2_no]['x'] + side
        phenotype[box1_no] = dict(x=max_found)
        #phenotype[gamma_x[i]]['x'] = max_found            
    
    """
    Calculates the y coordinates of boxes
    """    
    phenotype[gamma_y[n_boxes - 1]]['y'] = 0
    for i in range(n_boxes-2, -1, -1):
        max_found = 0
        box1_no = gamma_y[i]
        for j in range(i + 1, n_boxes):
            box2_no = gamma_y[j]
            side = 0
            if is_below(encoding[box2_no], encoding[box1_no]):
                if (orient[box2_no] == 0):
                    side = boxes[box2_no]['h']
                else:
                    side = boxes[box2_no]['w']
                if phenotype[box2_no]['y'] + side > max_found:
                    max_found = phenotype[box2_no]['y'] + side
        phenotype[box1_no]['y'] = max_found            
    
    individual['phenotype'] = phenotype
                             
def develop():    
    population = script_api.downloadIndividuals(
        select = ("gamma_x", "gamma_y", "orient", "encoding"), 
        where = (
            ("state", dc.EQUAL, constants.ALIVE),  
            ("gamma_x", dc.NOT_EQUAL, None),
            ("gamma_y", dc.NOT_EQUAL, None),
            ("orient", dc.NOT_EQUAL, None),
            ("encoding", dc.NOT_EQUAL, None),
            ("phenotype", dc.EQUAL, None)
        ), 
        count = constants.DEVELOPMENT_SIZE
    )


    #print "I am in development"    
    cur = time.time()
    
    for individual in population:
        calculate(individual)
    
    #print "development takes", time.time() - cur, "seconds."
    script_api.uploadIndividuals(population)
